// 获取当前日期并格式化
export const getCurrentDateFormatted = (separator: string = '-') => {
  const now = new Date();
  return formatDate(now, separator);
};

// 获取 指定 多少 个月前的日期并格式化
export const getMonthsAgoFormatted = (prevMonths: number = 3) => {
  const now = new Date();
  const fiveMonthsAgo = new Date(now); // 创建副本避免修改原日期
  fiveMonthsAgo.setMonth(fiveMonthsAgo.getMonth() - prevMonths);
  return formatDate(fiveMonthsAgo);
};

// 通用日期格式化函数
export const formatDate = (date: any, separator: string = '-') => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，需 +1
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}${separator}${month}${separator}${day}`;
};
// 时间戳格式化 YYYY-MM-DD
export function formatTimestamp(timestamp?: any) {
  let now = new Date();
  if (timestamp) {
    now = timestamp;
  }
  return new Date(now)
    .toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
    })
    .replace(/\//g, '-'); // 将默认的斜杠替换为横杠
}

/**
 * 获取指定日期的月份的第一天和最后一天
 * new Date(year, month, day)
 * year: 年份，四位数的整数值
 * month: 月份 从0开始计数，范围是0-11
 * day: 日期，从1开始计数，范围是1-31，具体取决于指定月份的天数。0表示上一个月的最后一天。
 * @param dateString 日期字符串
 * @returns
 */
export const getMonthStartAndEnd = (dateString: any) => {
  const date = dateString ? new Date(dateString) : new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  // const firstDay = new Date(year, month - 1, 1);
  const lastDay = new Date(year, month, 0);
  // padStart方法对两位数补零
  const formattedFirstDay = `${year}-${month.toString().padStart(2, '0')}-01`;
  const formattedLastDay = `${year}-${month.toString().padStart(2, '0')}-${lastDay.getDate()}`;
  return { firstDay: formattedFirstDay, lastDay: formattedLastDay };
};
// getMonthStartAndEnd('2023-11-15') { "firstDay": "2023-11-01", "lastDay": "2023-11-30" }

export const getMonthEndDay = (dateString: any) => {
  const date = dateString ? new Date(dateString) : new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const lastDay = new Date(year, month, 0);
  // padStart方法对两位数补零
  const formattedLastDay = `${year}-${month.toString().padStart(2, '0')}-${lastDay.getDate()}`;
  return formattedLastDay;
};
